import urllib
import xml.dom.minidom
import os
import time 
import zipfile
import re
import pyodbc
import libxml2

class CurvePoint:
    def __init__(self, tenor, rate):
        self.tenor = tenor
        self.rate = rate
    
cnxn = pyodbc.connect('driver={Vertica};server=ticino.ny.smsms.com;database=vdev01;uid=kala;pwd=jadu')
cursor = cnxn.cursor()
cursor.execute("CREATE TABLE TEST("
               "C_ID  INT,"
               "C_FP  FLOAT,"
               "C_VARCHAR VARCHAR(100),"
               "C_DATE DATE, C_TIME TIME,"
               "C_TS TIMESTAMP,"
               "C_BOOL BOOL)")
cursor.execute("SELECT IMPLEMENT_TEMP_DESIGN('TEST')")
cursor.execute("INSERT into test values(1,1.1,'abcdefg1234567890','1901-01-01','23:12:34','1901-01-01 09:00:09','t')")
values = (2,2.28,'abcdefg1234567890','1901-01-01','23:12:34','1901-01-01 09:00:09','t')
cursor.execute("INSERT into test values(?,?,?,?,?,?,?)",
               values[0], values[1], values[2], values[3], values[4], values[5], values[6])

cursor.execute("SELECT * FROM TEST")
rows = cursor.fetchall()
for row in rows:
    print row

cursor.execute("DROP TABLE TEST CASCADE")
cursor.close()
cnxn.close()

localtime = time.localtime()
datetoday = time.strftime("%Y%m%d", localtime)
print datetoday
datetoday = '20130215'
xmlstr = ''
localIRXMLZipFileName = "InterestRates_"+datetoday+".zip"
urlIRXMLFile = "http://www.marketerra.com/news/"+localIRXMLZipFileName
f = urllib.urlopen(urlIRXMLFile)
if os.path.isfile(localIRXMLZipFileName):
    os.remove(localIRXMLZipFileName)
if os.path.isfile(localIRXMLZipFileName):            
    print localIRXMLZipFileName+" already exist"
else:
    print "Saving file "+localIRXMLZipFileName
    urllib.urlretrieve(urlIRXMLFile, localIRXMLZipFileName)
    zfile = zipfile.ZipFile(localIRXMLZipFileName)
    for name in zfile.namelist():
        (dirname, filenameunzipped) = os.path.split(name)
        unzipFileName = filenameunzipped+''
        if unzipFileName != 'Disclaimer.txt':
            print "Unzipping " + filenameunzipped + " from " + localIRXMLZipFileName
            fd = open(filenameunzipped,"w")
            fd.write(zfile.read(name))
            fd.close()                
            fd = open(filenameunzipped,"r")
            xmlstr = fd.read()
            fd.close()
            print xmlstr
rc = []
def getText(nodelist, elemName = ''):
    elem = elemName
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
        else:
            if node.localName == 'tenor' or node.localName == 'parrate':
                getText(node.childNodes, node.localName)
    return elem.join(rc)

curvepoint = "curvepoint"
dom = xml.dom.minidom.parseString(xmlstr)
curvePoints = dom.getElementsByTagName(curvepoint)
for curvePointValue in curvePoints:
    curvePoint = getText(curvePointValue.childNodes, ':')
    rc = []
    print "Last curve point %s " % curvePoint
    cp = re.split(":", curvePoint)
    print cp[0]
    print cp[1]
